package com.leyou.service.impl;

import com.item.dto.CategoryDTO;
import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.entity.Category;
import com.leyou.mapper.CategoryMapper;
import com.leyou.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class CategoryServiceImpl implements CategoryService {
//    根据父节点查询商品类目
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public List<CategoryDTO> queryByParentId(Long pid) {

        Category record = new Category();
//        把
        record.setParentId(pid);
        List<Category> list = categoryMapper.select(record);

//        判断结果
        if(CollectionUtils.isEmpty(list)){
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOND);
        }
        return BeanHelper.copyWithCollection(list,CategoryDTO.class);
    }

    //通过id查询分类信息
    @Override
    public List<CategoryDTO> queryByBrandId(Long bid) {

//
        List<Category> categoris = categoryMapper.queryByBrandId(bid);

        if (CollectionUtils.isEmpty(categoris)){
//          空
            throw  new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }

//        转成CategoryDTO类型
        return BeanHelper.copyWithCollection(categoris,CategoryDTO.class);
    }

//    根据分类id的集合查询商品分类
    public List<CategoryDTO> queryCategoryByIds(List<Long> ids){
        List<Category> list = categoryMapper.selectByIdList(ids);
        if (CollectionUtils.isEmpty(list)) {
            // 没找到，返回404
            throw new LyException(ExceptionEnum.CATEGORY_NOT_FOUND);
        }
        return BeanHelper.copyWithCollection(list, CategoryDTO.class);
    }
}
